I need to find the pixel-frame for different ranges in a textview. I'm using the - (CGRect)firstRectForRange:(UITextRange *)range;
to do it. However I can't find out how to actually create a UITextRange
.
Basically this is what I'm looking for:
- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {
UITextRange*range2 = [UITextRange rangeWithNSRange:range]; //DOES NOT EXIST
CGRect rect = [textView firstRectForRange:range2];
return rect;
}
Apple says one has to subclass UITextRange
and UITextPosition
in order to adopt the UITextInput
protocol. I don't do that, but I tried anyway, following the doc's example code and passing the subclass to firstRectForRange
which resulted in crashing.
If there is a easier way of adding different colored UILables
to a textview, please tell me. I have tried using UIWebView with content editable
set to TRUE, but I'm not fond of communicating with JS, and coloring is the only thing I need.
Thanks in advance.
UITextRange
to be able to set it. The only way to setUITextRange
properties is to access them in the subclass. It definesstart
andend
as properties, but they can be set internally by referencing_start
and_end
. – Quicklyreadonly
properties. That is why this is a comment instead of an answer. – Quickly